package com.ch.dream.logic.main;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ch.dream.R;
import com.ch.dream.activity.main.RegisterActivity;
import com.ch.dream.asynctask.RegisterRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.UserinfoBean;
import com.ch.dream.common.CommonData;
import com.ch.dream.common.CommonResult;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.tools.UtilTools;
import com.ch.dream.tools.ViewTools;

public class RegisterLogic extends BaseLogic {
	
	private EditText etUserName;
	private EditText etPassword;
	private Button btnRegister;
	private String userName;
	private String password;
	private ProgressDialog loadingDialog;
	public RegisterLogic(RegisterActivity activity) {
		super(activity);
	}
	
	public void init(){
		initViews();
		btnRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(validate()){
					UserinfoBean bean =new UserinfoBean();
					bean.setUserName(userName);
					bean.setPassword(password);
					bean.setUserCode(CommonData.IMEI);
					new RegisterRequestAsyncTask(activity, new RequestServerListener() {
						@Override
						public void onRequestServerEnd(Object result) {
							try{
								loadingDialog.dismiss();
								if(result!=null){
									JSONObject jsonObject =(JSONObject)result;
									if(CommonResult.SUCCESS.equals(jsonObject.getString("result"))){
										ViewTools.showShortToast(activity, "注册成功");
										CommonUserInfo.userId =jsonObject.getString("id");
										CommonUserInfo.userName = userName;
										CommonUserInfo.password = password;
										UtilTools.setStringSharedPreferences(activity, CommonData.USERINFO, "userName", userName);
										UtilTools.setStringSharedPreferences(activity, CommonData.USERINFO, "password", password);
										activity.finish();
									}else if(CommonResult.FAIL.equals(jsonObject.getString("result"))){
										ViewTools.showMessage(activity, jsonObject.getString("message"));
									}else{
										ViewTools.showMessage(activity,"网络错误");
									}
								}
							}catch (Exception e) {
								e.printStackTrace();
								ViewTools.showMessage(activity,"网络错误");
							}
							
						}
						
						@Override
						public void onRequestServerBegin() {
							loadingDialog = ViewTools.showLoading(activity,"提示", "登录中，请稍候...");
						}
					}).execute(bean);
				}
			}
		});
	}
	
	public boolean validate(){
		
		if(TextUtils.isEmpty(etUserName.getText())){
			ViewTools.showShortToast(activity, "帐号不能为空");
			return false;
		}
		if(TextUtils.isEmpty(etPassword.getText())){
			ViewTools.showShortToast(activity, "密码不能为空");
			return false;
		}
		if(etPassword.getText().length()<6){
			ViewTools.showShortToast(activity, "密码至少为6位");
			return false;
		}
		userName = etUserName.getText().toString();
		password = etPassword.getText().toString();
		return true;
	}
	
	public void initViews(){
		btnRegister =(Button) activity.findViewById(R.id.btnRegister);
		etUserName =(EditText) activity.findViewById(R.id.etUserName);
		etPassword =(EditText) activity.findViewById(R.id.etPassword);
	}
}
